package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDihsMapper;

    @Autowired
    private DishMapper dishMapper;

    /**增加套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //将套餐数据(不包括菜品)插入到套餐表
        setmealMapper.addSetmeal(setmeal);
        //获取套餐id
        Long setmealId = setmeal.getId();
        //将菜品插入到套餐菜品表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }

        /*for (SetmealDish ss : setmealDishes) {
            setmealDishesMapper.add(ss);
        }*/
        //批量插入
        setmealDihsMapper.addSetmealdish(setmealDishes);



    }

    /**套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //返回数据
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> records = page.getResult();
        return new PageResult(total,records);
    }

    /**批量删除套餐
     *
     * @param ids
     */

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //判断能否删除
        //判断套餐是否在售
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectByid(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //没有在售，可执行删除，同时把内部菜品删除。此处使用批量删除
        setmealMapper.deleteBySetmealId(ids);
        //删除内部菜品
        setmealDihsMapper.deleteBySetmealId(ids);
    }

    /**回显数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDihsMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**修改套餐信息
     *
     * @param setmealDTO
     */

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐基础信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //获取套餐ID
        Long setmealId = setmeal.getId();
        //修改套餐中的菜品信息 先把其中的菜品全部删除，在重新插入
        //删除
        Long id = setmealDTO.getId();
        List<Long> list = new ArrayList<>();
        list.add(id);
        setmealDihsMapper.deleteBySetmealId(list);

        //重新插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            if (setmealDish !=null && setmealDishes.size()>0){
                //设置套餐ID
                setmealDish.setSetmealId(setmealId);
            }

        }
        //批量插入
        setmealDihsMapper.addSetmealdish(setmealDishes);

    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
//        Setmeal setmeal = Setmeal.builder()
//                .status(status)
//                .id(id)
//                .build();
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        //停售
        if (status == StatusConstant.DISABLE){
            setmealMapper.update(setmeal);
        }
        //起售
        if (status == StatusConstant.ENABLE){
            //拿到套餐中所有的菜品对象
            List<SetmealDish> setmealDishes = setmealDihsMapper.getById(id);
            for (SetmealDish setmealDish : setmealDishes) {
                //拿到某个菜品的菜品id
                Long dishId = setmealDish.getDishId();
                //拿到上述id 的菜品信息
                Dish dish = dishMapper.getById(dishId);
                //拿到该菜品的状态
                Integer dishStatus = dish.getStatus();
                //判断菜品状态
                if (dishStatus == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
            //未抛出异常，正确执行
            setmealMapper.update(setmeal);
        }
    }

    @Override
    public List<Setmeal> getSetmealByCategoryId(Long categoryId) {
        //查询套餐
        List<Setmeal> list = setmealMapper.getSetmealByCategoryId(categoryId);
        return list;
    }

    /**根据套餐id查询包含的菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getSetmealDishBySetmealId(Long id) {
        //根据套餐id查询包含的菜品,
        List<DishItemVO> dishItemVOS = setmealDihsMapper.getSetmealDishBySetmealId(id);
        return dishItemVOS;
    }

}

